const app = getApp();

Page({
  data: {
    id: null,
    reservation: null,
    loading: false
  },
  
  onLoad: function(options) {
    // 检查登录状态
    if (!app.checkLogin()) {
      return;
    }
    
    const id = options.id;
    this.setData({
      id: id
    });
    
    // 加载预约详情
    this.loadReservationDetail();
  },
  
  // 加载预约详情
  loadReservationDetail: function() {
    wx.showLoading({
      title: '加载中...',
    });
    
    app.request({
      url: `/reservations/${this.data.id}`
    }).then(data => {
      wx.hideLoading();
      this.setData({
        reservation: data
      });
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message || '加载失败',
        icon: 'none'
      });
      
      // 返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    });
  },
  
  // 取消预约
  cancelReservation: function() {
    wx.showModal({
      title: '提示',
      content: '确认取消该预约吗？',
      success: (res) => {
        if (res.confirm) {
          this.performCancelReservation();
        }
      }
    });
  },
  
  // 执行取消预约
  performCancelReservation: function() {
    if (this.data.loading) return;
    
    this.setData({
      loading: true
    });
    
    wx.showLoading({
      title: '取消中...',
    });
    
    app.request({
      url: `/reservations/${this.data.id}/cancel`,
      method: 'POST'
    }).then(data => {
      wx.hideLoading();
      this.setData({
        loading: false,
        reservation: data
      });
      
      wx.showToast({
        title: '取消成功',
        icon: 'success'
      });
    }).catch(err => {
      wx.hideLoading();
      this.setData({
        loading: false
      });
      
      wx.showToast({
        title: err.message || '取消失败',
        icon: 'none'
      });
    });
  },
  
  // 签到
  signIn: function() {
    wx.showActionSheet({
      itemList: ['二维码签到', '位置签到'],
      success: (res) => {
        if (res.tapIndex === 0) {
          this.qrCodeSignIn();
        } else if (res.tapIndex === 1) {
          this.locationSignIn();
        }
      }
    });
  },
  
  // 二维码签到
  qrCodeSignIn: function() {
    wx.scanCode({
      success: (res) => {
        // 处理扫码结果
        this.performSignIn('qrcode', res.result);
      },
      fail: (err) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  },
  
  // 位置签到
  locationSignIn: function() {
    // 不再使用高德地图，直接执行签到
    this.performSignIn('location', null, { latitude: 0, longitude: 0 });
  },
  
  // 执行签到
  performSignIn: function(signInMethod, qrCodeData, location) {
    if (this.data.loading) return;
    
    this.setData({
      loading: true
    });
    
    wx.showLoading({
      title: '签到中...',
    });
    
    const data = {
      signInMethod: signInMethod
    };
    
    if (qrCodeData) {
      data.qrCodeData = qrCodeData;
    }
    
    if (location) {
      data.location = location;
    }
    
    app.request({
      url: `/reservations/${this.data.id}/sign-in`,
      method: 'POST',
      data: data
    }).then(data => {
      wx.hideLoading();
      this.setData({
        loading: false,
        reservation: data
      });
      
      wx.showToast({
        title: '签到成功',
        icon: 'success'
      });
    }).catch(err => {
      wx.hideLoading();
      this.setData({
        loading: false
      });
      
      wx.showToast({
        title: err.message || '签到失败',
        icon: 'none'
      });
    });
  }
}) 